layui.use(['form', 'admin', 'ax','upload','formSelects'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var upload = layui.upload;
    var formSelects = layui.formSelects;

    // 存储已上传的图片路径
    var uploadedImages = [];

    // 加载官方用户列表
    var officialUsersAjax = new $ax(Feng.ctxPath + "/nanUser/officialList");
    var officialUsersRes = officialUsersAjax.start();
    if(officialUsersRes.success && officialUsersRes.data){
        var userHtml = "<option value=''>-请选择发布账号-</option>";
        for(var i = 0; i < officialUsersRes.data.length; i++){
            var user = officialUsersRes.data[i];
            userHtml += '<option value="'+user.id+'">'+user.nickName+'</option>';
        }
        $("#userId").html(userHtml);
        form.render('select');
    }

    // 多图片上传
    upload.render({
        elem: '#uploadThumb'
        ,url: Feng.ctxPath + '/system/upload'
        ,multiple: true
        ,done: function(res){
            if(res.success){
                uploadedImages.push(res.data.finalName);
                // 更新预览
                var imgHtml = '<img src="' + Feng.cdn + res.data.finalName + '" class="layui-upload-img">';
                $('#thumbPreview').append(imgHtml);
                // 更新隐藏字段
                $('#thumb').val(uploadedImages.join(','));
                Feng.success("上传成功！");
            }else{
                Feng.error("上传失败！" + res.message)
            }
        }
        ,error: function(){
            Feng.error("上传失败！");
        }
    });

    // 表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        // 验证富文本内容
        var content = ue.getContent();
        if(!content || content.trim() === ''){
            Feng.error("请输入帖子内容！");
            return false;
        }

        var ajax = new $ax(Feng.ctxPath + "/nanSellAnnounce/addItem", function (data) {
            if(data.success){
                Feng.success("发布成功！");
                // 传给上个页面，刷新table用
                admin.putTempData('formOk', true);
                // 关掉对话框
                admin.closeThisDialog();
            }else{
                Feng.error("发布失败！" + data.message)
            }
        }, function (data) {
            Feng.error("发布失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });

    // 实例化编辑器
    var ue = UE.getEditor('container', {
        enableAutoSave: false,
        autoHeightEnabled: true,
        autoFloatEnabled: false,
        imageUrlPrefix: Feng.cdn,
        fileUrlPrefix: Feng.cdn,
        videoUrlPrefix: Feng.cdn,
        scaleEnabled: true,         // 滚动条
        initialFrameHeight: 400     // 默认的编辑区域高度
    });

    UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
    UE.Editor.prototype.getActionUrl = function (action) {
        if (action === 'uploadimage' || action === 'uploadscrawl' || action === 'uploadimage') {
            return Feng.ctxPath + '/ueditor/imgUpdate';
        } else if (action === 'uploadfile') {
            return Feng.ctxPath + '/ueditor/uploadfile';
        } else if (action === 'uploadvideo') {
            return Feng.ctxPath + '/ueditor/uploadvideo';
        } else {
            return this._bkGetActionUrl.call(this, action);
        }
    };
});
